Ext.define('Ext.desk.Control.Header',{
	extend: 'Ext.form.Panel',
	alias: 'widget.deskheader',
	frame: false,
	baseCls: 'x-plain',
    initComponent: function () {
        var me = this;
        me.frame = false;
        me.bbar = Ext.create('Ext.panel.Panel',{
    		xtype: 'panel',
    		layout: 'column',
    		baseCls: 'x-plain',
    		defaults: {columnWidth: 1},
    		items: [me.getMenuPanel(),me.getMapPanel()],
    		height: 59
        });
        
        if(!Ext.isChrome){
        	me.html = '<div style="position: absolute;top:0px;right:0px;color:red;">系统检测到你的浏览器反应缓慢，建议下载Chrome浏览器。<a href="tool/browser.rar">点此下载</a></div>';
        }
        
        me.callParent();
    },
    getMenuPanel: function(){
    	var me = this;
    	return {
        	xtype: 'panel',
        	border: true,
        	height: 30,
        	baseCls: 'x-plain',
            layout: {
                type: 'hbox',
                align: 'middle'
            },
            defaults: {
            	xtype: 'box',
            	style: {cursor: 'pointer'},
            	height: 30,
            	width: 101,
            	listeners: {
            		boxready: function(c){
            			c.getEl().on('click',function(e,html){
            				if(c.menuid){
            					desk.down('desknavigationpanel').store.load({
                					params: {node: c.menuid}
                				});
            					
            					Ext.getCmp('sys-desk-nav-c').setTitle(c.name);
            				}
            				else if(c.flag == 'xgmm'){
            					me.changePwd();
            				}
            				else if(c.flag == 'gxsz'){
            					me.setUserConfig();
            				}
            				else if(c.flag == 'tc'){
            					me.safeExit();
            				}
            			});
            		}
            	}
            },
            items: [{
            	xtype: 'tbfill',
            	baseCls: 'x-plain'
            },{
                name: '首页',
                menuid: -7,
                width: 115,
                id:me.id+"sy",
                cls: 'menu-sy',
                hide: true,
                overCls: 'menu-sy-hover'
            },{
                name: '系统管理',
                menuid: -1,
                id:me.id+"xtgl",
                hidden:true,
                cls: 'menu-xtgl',
                overCls: 'menu-xtgl-hover'
            },{
            	name: '基础信息',
                menuid: -2,
                id:me.id+"jcgl",
                hidden:true,
                cls: 'menu-jcxx',
                overCls: 'menu-jcxx-hover'
            },{
            	name: '任务管理',
                menuid: -3,
                id:me.id+"rwgl",
                hidden:true,
                cls: 'menu-rwgl',
                overCls: 'menu-rwgl-hover'
            },{
            	name: '视频管理',
                menuid: -4,
                id:me.id+"spgl",
                hidden:true,
                cls: 'menu-spgl',
                overCls: 'menu-spgl-hover'
            },{
            	name: '违规管理',
                menuid: -5,
                id:me.id+"wggl",
                hidden:true,
                cls: 'menu-wggl',
                overCls: 'menu-wggl-hover'
            },{
            	name: '数据分析',
                menuid: -6,
                id:me.id+"sjfx",
                hidden:true,
                cls: 'menu-sjfx',
                overCls: 'menu-sjfx-hover'
            },{
            	name: '个性设置',
                flag: 'gxsz',
                cls: 'menu-gxsz',
                overCls: 'menu-gxsz-hover'
            },{
            	name: '修改密码',
                flag: 'xgmm',
                cls: 'menu-xgmm',
                overCls: 'menu-xgmm-hover'
            },{
            	name: '退出',
                flag: 'tc',
                margin: '0 20 0 0',
                width: 115,
                cls: 'menu-aqtc',
                overCls: 'menu-aqtc-hover'
            }],
            listeners: {
            	boxready: function(p){
	        		 Ext.Ajax.request({
	                     url: 'menu/getNavigationMenu.do',
	                     success: function (response, options) {
	                    	 var data = Ext.decode(response.responseText);
	                    	 if(user.id ==0){
                				 Ext.getCmp(me.id+'xtgl').show();
                				 Ext.getCmp(me.id+'jcgl').show();
                				 Ext.getCmp(me.id+'rwgl').show();
                				 Ext.getCmp(me.id+'spgl').show();
                				 Ext.getCmp(me.id+'wggl').show();
                				 Ext.getCmp(me.id+'sjfx').show();
                				 return;
	                    	 }else{
	                    		 for ( var i = 0; i < data.length; i++) {
	                    			 var menu = data[i].menu;
	                    			 if(menu == -1){
	                    				 Ext.getCmp(me.id+'xtgl').show();
	                    			 }
	                    			 if(menu == -2){
	                    				 Ext.getCmp(me.id+'jcgl').show();
	                    			 }
	                    			 if(menu == -3){
	                    				 Ext.getCmp(me.id+'rwgl').show();
	                    			 }
	                    			 if(menu == -4){
	                    				 Ext.getCmp(me.id+'spgl').show();
	                    			 }
	                    			 if(menu == -5){
	                    				 Ext.getCmp(me.id+'wggl').show();
	                    			 }
	                    			 if(menu == -6){
	                    				 Ext.getCmp(me.id+'sjfx').show();
	                    			 }
	                    		 }
	                    	 }
	                     }
	                 });
            	}
            }
        };
    },
    getMapPanel: function(){
    	return {
        	xtype: 'panel',
        	border: true,
        	height: 29,
        	baseCls: 'x-plain',
        	margin: 0,
        	padding: 0,
        	bodyPadding: 0,
        	layout: {
                type: 'hbox',
                align: 'middle'
            },
            defaults: {baseCls: 'x-plain',height: 29},
            items: [{
            	xtype: 'label',
            	width: 10,
            	style: {background: 'url(images/index/mb_1.png) no-repeat'}
            },{
            	xtype: 'label',
            	width: 200,
            	id: 'desk-map',
            	text: '当前位置：首页>>首页警示',
            	style: {background: 'url(images/index/mb_2.png)','line-height': '200%'}
            },{
            	xtype: 'tbfill',
            	style: {background: 'url(images/index/mb_2.png)'}
            },{
            	xtype: 'label',
            	width: 170,
            	text: user.username+',欢迎您进入平台！',
            	style: {background: 'url(images/index/mb_2.png)','line-height': '200%'}
            },{
            	xtype: 'label',
            	width: 200,
            	style: {background: 'url(images/index/mb_2.png)','line-height': '200%'},
            	id: 'desk-time',
            	listeners: {afterrender: function(){show_sys_time();}}
            },{
            	xtype: 'label',
            	width: 10,
            	style: {background: 'url(images/index/mb_3.png) no-repeat'}
            }]
        };
    },
    changePwd: function(){
    	var me = this;
    	Ext.create('Ext.window.Window',{
			title: user.username+'-修改密码',
			width: 400,
			height: 249,
			modal: true,
			layout: 'fit',
			buttonAlign: 'center',
			items: [{
				xtype: 'form',
				layout: 'column',
				border: false,
				bodyPadding: 15,
				defaults: {columnWidth: 1,margin: 5,labelWidth: 65,allowBlank: false},
				items: [{
					fieldLabel: '账号',
					xtype: 'textfield',
					name: 'account',
					value: user.account,
					readOnly: true
				},{
					fieldLabel: '旧密码',
					xtype: 'textfield',
					name: 'oldpwd',
					inputType: 'password',
					maxLength:25
				},{
					fieldLabel: '新密码',
					xtype: 'textfield',
					name: 'newpwd0',
					inputType: 'password',
					maxLength:25
				},{
					fieldLabel: '确认密码',
					xtype: 'textfield',
					name: 'newpwd1',
					inputType: 'password',
					maxLength:25
				},{
					fieldLabel: '验证码',
					xtype: 'textfield',
					columnWidth: .81,
					name: 'vcode'
				},{
					xtype: 'box',
					id:'meupdatepwd',
					columnWidth: .19,
					height: 24,
					refresh: function(dom){
						dom.src="user/getVimg.do?id="+new Date().getTime();
					},
					listeners: {
						beforerender: function(box){
							box.html = '&nbsp;&nbsp;&nbsp;&nbsp;<img style="height:25px;position:absolute; top:146px; left:308px;cursor:hand" src="'
								+ 'user/getVimg.do?id='+new Date().getTime() + ' title="点击刷新" onclick="Ext.getCmp(\''+box.id+'\').refresh(this);"></img>';
						}
					}
				}]
			}],
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('window').down('form');
        	    	var form = formp.form;
        	    	var formdata = form.getValues();
        	    	var oldpwd = form.findField('oldpwd').getValue();
        	    	var newpwd0 = form.findField('newpwd0').getValue();
        	    	var newpwd1 = form.findField('newpwd1').getValue();
        	    	formdata.oldpwd = hex_sha1(oldpwd);
        	    	formdata.newpwd0 = hex_sha1(newpwd0);
        	    	formdata.newpwd1 = hex_sha1(newpwd1);
        	    	if(form.isValid()){
//        	    		form.submit({
//            	    	    url: 'user/changePwd.do',
//            	    	    success: function(form, action) {
//            	    	    	var rs = Ext.decode(action.response.responseText);
//            	    	    	if(rs.rst){
//            	    	    		Ext.Msg.alert('提示', '保存成功！');
//            	    	    		formp.up('window').close();
//            	    	    	}
//            	    	    	else{
//            	    	    		Ext.Msg.alert('提示', '保存失败！详情：<br>'+rs.msg);
//            	    	    	}
//            	    	    }
//            	    	});
        	    		Ext.Ajax.request({
						    url: 'user/changePwd.do',
						    params:formdata,
						    success: function(response){
						        var rs = Ext.decode(response.responseText);
						        if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：<br>'+rs.msg);
            	    	    	}
						    }
						});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '请填写完整的表单！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}]
		}).show();
    },
    setUserConfig: function(){
    	var me = this;
    	var configer = me.configer;
		if(!configer){
			configer = me.configer = Ext.create('Ext.desk.Control.Configer');
		}
		configer.show();
    },
    safeExit: function(){
    	Ext.Msg.confirm('安全退出', '您确定要退出系统吗?',function(b){
        	if(b == 'yes'){
        		Ext.Ajax.request({
            	    url: 'user/logout.do'
            	});
        		window.location = basePath+'login.jsp';
        	}
        });
    }
});